package com.aidex.web.controller.api;

import com.aidex.biz.domain.Order;
import com.aidex.biz.domain.OrderImage;
import com.aidex.biz.service.*;
import com.aidex.common.config.AiDexConfig;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.utils.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 小程序首页管理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-21
 */
@RestController("ApiQingYangOrderController")
@RequestMapping("/api/qyOrder")
@Api(tags = "小程序首页管理接口")
@Component
public class QingYangOrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderImageService orderImageService;

    @Autowired
    private JdKuaiyunService jdKuaiyunService;
    @Autowired
    private JdkyOrderService jdkyOrderService;
    @Autowired
    private QingYangOrderService qingYangOrderService;

    /**
     * 订单取货单号
     */
    @PostMapping("/delivery")
    public R delivery(@RequestBody JSONObject param) throws Exception {
        if (StringUtils.isNotEmpty(param.getString("qingyangOrderNumber"))) {
            Order order = new Order();
            order.setQingyangOrderId(param.getString("qingyangOrderNumber"));
            order.setDeliveryNo(param.getString("originalLogisticsNumber"));
            Order oldOrder=orderService.getByQinYangId(order);
            if(!oldOrder.getCompanyId().equals(AiDexConfig.getCompanyId())){
                return R.status(orderService.updateQyOrderDelivery(order));
            }else{
                return R.status(true);
            }
        } else {
            return R.status(true);
        }
    }

    /**
     * 订单送件单号
     */
    @PostMapping("/receive")
    public R receive(@RequestBody JSONObject param) throws Exception {
        if (StringUtils.isNotEmpty(param.getString("qingyangOrderNumber"))) {
            Order order = new Order();
            order.setQingyangOrderId(param.getString("qingyangOrderNumber"));
            order=orderService.getByQinYangId(order);
            if(AiDexConfig.getCompanyId().equals(order.getCompanyId())){
                order.setReceiveNo(orderService.pushJdKuaiYun(order,""));
            }else{
                order.setReceiveNo(param.getString("originalLogisticsNumber"));
            }
            return R.status(orderService.updateQyOrderReceive(order));
        } else {
            return R.fail(param.toString());
        }
    }


    /**
     * 订单衣物信息
     */
    @PostMapping("/clothes")
    public R clothes(@RequestBody JSONObject param) {
        Order order = new Order();
        int index = 0;
        if (StringUtils.isNotEmpty(param.getString("qingyangOrderNumber"))) {
            order.setQingyangOrderId(param.getString("qingyangOrderNumber"));
            JSONArray jsonArray = param.getJSONArray("details");
            List<OrderImage> orderImages = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONArray signPhotoList = jsonArray.getJSONObject(i).getJSONArray("signPhotoList");
                for (int j = 0; j < signPhotoList.size(); j++) {
                    OrderImage orderImage = new OrderImage();
                    orderImage.setQingyangOrderId(order.getQingyangOrderId());
                    orderImage.setImage(signPhotoList.getString(j));
                    orderImage.setBusType(1L);// 0 揽件图片 1分拣图片
                    orderImage.setType(0L);// 0 清洗前 1 清洗后
                    orderImages.add(orderImage);
                }
            }
            index = orderImageService.batchInsert(orderImages);
            // 修改订单状态 清洗中
            order.setStatus("5");
            orderService.updateByQinXi(order);
        }
        return R.status(index);
    }

}







